package com.coship.vasms.adstorm.common;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;

import com.coship.vasms.adstorm.common.log.JLogger;
import com.coship.vasms.adstorm.common.log.LoggerFactory;

/**
 * 
 * 
 * File Name : SystemConfig.java
 * 
 * @Description : 系统配置文件类
 * @author 903329
 */
public class SystemConfig {
	private static final JLogger logger = LoggerFactory
			.getLogger(SystemConfig.class);
	private static SystemConfig instance = null;
	private static Configuration configuration = null;// 这里采用Configuration以便以后扩展XML等其他配置文件

	/**
	 * 系统配置文件名称
	 */
	private static final String SYSTEM_CONFIG_FILE_NAME = "sysconfig.properties";

	public static synchronized SystemConfig getInstance() {
		if (instance == null) {
			synchronized (SystemConfig.class) {
				if (instance == null) {
					instance = new SystemConfig();
				}
			}

		}
		return instance;
	}

	/**
	 * 
	 * Description : 提供几个获取配置信息的常用方法
	 * 
	 * @param key
	 * @return
	 * 
	 */
	public String getString(String key) {
		return configuration.getString(key);

	}

	/**
	 * 
	 * Description : 提供几个获取配置信息的常用方法
	 * 
	 * @param key
	 * @return
	 * 
	 */
	public int getInt(String key) {
		return configuration.getInt(key);

	}

	/**
	 * 
	 * Description : 提供几个获取配置信息的常用方法
	 * 
	 * @param key
	 * @return
	 * 
	 */
	public String[] getStringArray(String key) {
		return configuration.getStringArray(key);

	}

	/**
	 * 
	 * Description : 提供几个获取配置信息的常用方法
	 * 
	 * @param key
	 * @return
	 * 
	 */
	public Boolean getBoolean(String key) {
		return configuration.getBoolean(key);

	}

	/**
	 * 
	 * Description : 提供几个获取配置信息的常用方法
	 * 
	 * @param key
	 * @return
	 * 
	 */
	public Object getProperty(String key) {
		return configuration.getProperty(key);

	}

	private SystemConfig() {
		load();
	}

	/**
	 * 
	 * Description : 加载配置文件信息
	 * 
	 * 
	 */
	private void load() {
		// 读配置文件
		try {
			configuration = new PropertiesConfiguration(CommonUtils
					.getConfigPath()
					+ CommonUtils.getSysFileSeparator()
					+ SYSTEM_CONFIG_FILE_NAME);
			if (configuration == null) {
				logger.error("系统配置文件" + SYSTEM_CONFIG_FILE_NAME + "不存在!");
				throw new IllegalArgumentException("系统配置文件"
						+ SYSTEM_CONFIG_FILE_NAME + "不存在!");
			}
		} catch (ConfigurationException e) {
			// TODO Auto-generated catch block
			logger.error("读系统配置文件" + SYSTEM_CONFIG_FILE_NAME + "出现错误!");
			throw new IllegalArgumentException("读系统配置文件"
					+ SYSTEM_CONFIG_FILE_NAME + "出现错误!");
		}
	}

	public static void main(String args[]) {
		System.out.println("aa=" + SystemConfig.getInstance().getString("aa"));

	}
}
